package soar.test.image;

import java.io.File;

public class App
{
	private final static String __CLASS__ = App.class.getName();

	public static void main(String[] args)
	{
		@SuppressWarnings("unused")
		final String __METHOD__ = "main";

		System.exit(new App().run());
	}

	private int run()
	{
		final String __METHOD__ = "run";

		System.out.println("begin");

		try
		{
			init_output();

			WeatherData weather_data = new WeatherData();
			weather_data.random();
			String bg_img_file = "草地.jpg";
			String ad_img_file = "蒙牛右侧广告图.jpg";
			byte[] image = ImageUtil.merge(weather_data, bg_img_file,
			        ad_img_file);
			System.out.println(String.format(
			        "%s.%s: merge succeed, image.len:%d", __CLASS__,
			        __METHOD__, image.length));
			byte[] video = ImageUtil.convertToVideo(image, "MPEG2",
			        Conf.process.output);
			System.out.println(String.format(
			        "%s.%s: convert MPEG2 succeed, video.len:%d",
			        __CLASS__, __METHOD__, video.length));
			video = ImageUtil.convertToVideo(image, "MPEG4",
			        Conf.process.output);
			System.out.println(String.format(
			        "%s.%s: convert MPEG4 succeed, video.len:%d",
			        __CLASS__, __METHOD__, video.length));
			video = ImageUtil.convertToVideo(image, "H264",
			        Conf.process.output);
			System.out.println(String.format(
			        "%s.%s: convert H264 succeed, video.len:%d",
			        __CLASS__, __METHOD__, video.length));
		}
		catch (Exception e)
		{
			System.out.println(String.format("%s.%s: fail, %s", __CLASS__,
			        __METHOD__, e));
			return 1;
		}

		System.out.println("end");

		return 0;
	}

	private boolean init_output() throws Exception
	{
		final String __METHOD__ = "init_output";

		String dir = Conf.process.output;
		String abdir;
		if (dir.substring(0, 1).equals("/"))
		{
			abdir = dir;
		}
		else
		{
			abdir = (new File(".").getAbsolutePath()) + "/" + dir;
		}
		File d = new File(abdir);
		if (d.exists())
		{
			if (!d.isDirectory() || !d.canRead() || !d.canWrite()
			        || !d.canExecute())
			{
				throw new Exception(String.format("%s: dir(%s) invalid",
				        __METHOD__, abdir));
			}
		}
		else
		{
			if (!d.mkdir())
			{
				throw new Exception(String.format(
				        "%s: mkdir dir(%s) fail", __METHOD__, abdir));
			}
		}

		return true;
	}
}
